/*
 * Copyright 2008-2009 ArtisanLogic.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.artisanlogic.jingo.assembler;

import static com.artisanlogic.lang.Utils.requireParameter;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.artisanlogic.jingo.parser.Parser;

// =====================================================================================================================
// Scanner.java                                                                                  Created on Feb 18, 2009
// ---------------------------------------------------------------------------------------------------------------------
/**
 * This class scans pages and page templates to locate root Jingo module declarations in order to drive the assembly 
 * process.
 * 
 * @author sean@artisanlogic.com
 * @author john@artisanlogic.com
 * @since 0.9-Beta
 */
// =====================================================================================================================
public class Scanner {

  // -------------------------------------------------------------------------------------------------------------------
  // private instance fields
  // -------------------------------------------------------------------------------------------------------------------

  private final UrlMappingStrategy urlMapper;

  private final File outputDir;

  private final Pattern pattern;

  private final Parser parser;

  // -------------------------------------------------------------------------------------------------------------------
  // constructors
  // -------------------------------------------------------------------------------------------------------------------

  /**
   * This constructor creates a new Scanner with the specified values for the urlMapper and outputDir properties.
   * 
   * @param urlMapper This parameter holds the URL mapping strategy to be used to map repository prefixes to actual 
   *  universal resource locators.
   * @param outputDir This parameter holds the File representation of the directory that Jingo module dependency graph 
   *  assemblies should be written to.
   */
  public Scanner(UrlMappingStrategy urlMapper, Parser parser, File outputDir) {
    this(urlMapper, parser, outputDir, "<script\\b[^>]*>(.*?)</script>");
  }

  /**
   * This constructor creates a new Scanner with the specified values for the ... properties.
   * 
   * @param urlMapper This parameter holds the URL mapping strategy to be used to map repository prefixes to actual 
   *  universal resource locators.
   * @param outputDir This parameter holds the File representation of the directory that Jingo module dependency graph 
   *  assemblies should be written to.
   * @param pattern This parameter holds a String representation of the regular expression pattern that identifies and 
   *  captures JavaScript content within the page or page template source to be scanned.
   */
  public Scanner(UrlMappingStrategy urlMapper, Parser parser, File outputDir, String pattern) {
    requireParameter("urlMapper", urlMapper);
    this.urlMapper = urlMapper;
    requireParameter("parser", parser);
    this.parser = parser;
    requireParameter("outputDir", outputDir);
    this.outputDir = outputDir;
    requireParameter("pattern", pattern);
    this.pattern = Pattern.compile(pattern, Pattern.MULTILINE | Pattern.DOTALL);
  }

  // -------------------------------------------------------------------------------------------------------------------
  // public instance methods
  // -------------------------------------------------------------------------------------------------------------------

  /**
   * This method scans the specified page or page template source for JavaScript source content and Delegates to a 
   * FileHandler instance to process any root Jingo module declarations that may occur within any discovered chunks of 
   * JavaScript.
   * 
   * @param source This parameter holds the source of the page or page template that is currently being scanned for root
   *  Jingo module declarations.
   */
  public void scan(String source) {
    requireParameter("source", source);
    for(Matcher matcher = pattern.matcher(source); matcher.find();) {
      parser.parse(matcher.group(1), new FileHandler(urlMapper, parser, outputDir), "main");
    }
  }

}
